<?php
/**
 *Copyright (c) <2017> <copyright taodingfei>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

namespace Themis\Container;

use Themis\Container\Dependency\ClosureDependency;
use Themis\Container\Dependency\ObjectDependency;
use Themis\Container\Dependency\StringDependency;
use Themis\Contracts\Container\ContainerInterface;
use Themis\Contracts\Container\Service\ServiceInterface;

class Container implements ContainerInterface
{
    /**
     * @var ObjectDependency $instances
     */
    protected $instances;

    /**
     * @var ClosureDependency $closures
     */
    protected $closures;

    /**
     * @var StringDependency $strings
     */
    protected $strings;

    public function __construct()
    {
        $this->instances=new ObjectDependency();
        $this->closures=new ClosureDependency();
        $this->strings=new StringDependency();
    }

    /**
     * 从容器中解析出$abstract对象
     *
     * @param string $abstract
     * @return mixed
     */
    public function make($abstract)
    {
        if(!empty($object=$this->instances->get($abstract))){
            return $object;
        }

        if(!empty($class=$this->strings->get($abstract))){
            return $this->build($class);
        }

        if(!empty($closure=$this->closures->get($abstract))){
            return $this->call($closure);
        }
    }

    /**
     * 判断容器中是否存在$abstract依赖
     *
     * @param string $abstract
     * @return bool
     */
    public function has($abstract)
    {
        return $this->instances->has($abstract) ||
            $this->strings->has($abstract) ||
            $this->closures->has($abstract);
    }

    /**
     * 使用依赖注入实例化$abstract，仅支持类名的形式
     *
     * @param string $abstract
     * @param array|null $parameters
     * @return mixed
     */
    public function build($abstract, $parameters = array())
    {
        return ResolverFactory::create($this, $abstract)->build($parameters);
    }

    /**
     * 使用依赖注入调用$callable，模拟call_user_func函数,
     * 支持Closure和class@method的两种格式
     *
     * @param callable $callable
     * @param array $parameters
     * @return mixed
     */
    public function call($callable, $parameters = array())
    {
        return ResolverFactory::create($this, $callable)->build($parameters);
    }

    /**
     * 向容器中注入依赖关系
     *
     * @param string $abstract
     * @param null|callable|object|string $concrete
     * @param bool $shared
     */
    public function bind($abstract, $concrete = null, $shared = false)
    {
        if(is_null($concrete)){
            if($shared){
                $this->instances->set($abstract, $this->build($abstract));
            }else{
                $this->strings->set($abstract, $abstract);
            }
        }

        if($concrete instanceof \Closure){
            if($shared){
                $this->instances->set($abstract, $this->call($concrete));
            }else{
                $this->closures->set($abstract, $concrete);
            }
        }

        if(is_string($concrete)){
            if($shared){
                $this->instances->set($abstract,$this->build($concrete));
            }else{
                $this->strings->set($abstract, $concrete);
            }
        }

        if(is_object($concrete)){
            $this->instances->set($abstract, $concrete);
        }
    }

    /**
     * 以共享的方式向容器注入依赖
     *
     * @param string $abstract
     * @param null|callable|object $concrete
     */
    public function share($abstract, $concrete = null)
    {
        $this->bind($abstract, $concrete, true);
    }

    /**
     * 向容器中注册服务
     *
     * @param ServiceInterface $service
     * @return void
     */
    public function registerService(ServiceInterface $service)
    {
        // TODO: Implement registerService() method.
    }
}